package cn.tedu.egmvc3.controller;

import cn.tedu.egmvc3.base.config.base.response.JsonResult;
import cn.tedu.egmvc3.base.config.base.response.StatusCode;
import cn.tedu.egmvc3.mapper.UserMapper;
import cn.tedu.egmvc3.pojo.dto.UserAddParam;
import cn.tedu.egmvc3.pojo.dto.UserUpdateParam;
import cn.tedu.egmvc3.pojo.entity.User;
import cn.tedu.egmvc3.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Api(tags = "01.用户管理模块")
@Slf4j
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /**
     * 1.添加用户功能
     */
    @PostMapping("add")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 10)
    public JsonResult add(UserAddParam userAddParam){
        log.debug("userAddParam=" + userAddParam);

        User user = new User();
        BeanUtils.copyProperties(userAddParam, user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        int i = userMapper.add(user);
        if (i>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED,user);
        }
    }

    /**
     * 2.用户列表功能
     */
    @GetMapping("list")
    @ApiOperation("用户列表")
    @ApiOperationSupport(order = 20)
    public List<UserListVO> list(){
        return userMapper.list();
    }

    /**
     * 3.删除用户功能
     */
    @PostMapping("del")
    @ApiOperation("删除用户")
    @ApiOperationSupport(order = 30)
    public JsonResult del(Long id){
        log.debug("id = " + id);
        int i = userMapper.del(id);
        if (i>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED,del(null));
        }
    }

    /**
     * 4.更新指定用户功能
     */
    @PostMapping("update")
    @ApiOperation("更新用户")
    @ApiOperationSupport(order = 40)
    public JsonResult update(UserUpdateParam userUpdateParam){
       log.debug("userUpdateParam = " + userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam, user);
        user.setUpdateTime(new Date());
        int i = userMapper.update(user);

        if (i>0){
            return JsonResult.ok();
        } else {
           return new JsonResult(StatusCode.OPERATION_FAILED,update(null));
        }
    }
}












